package cn.yunyichina.provider.clinic.service.impl;

import java.io.File;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;
import cn.yunyichina.provider.framework.recipe.util.RecipePicUtil;
import cn.yunyichina.provider.iface.entity.clinic.RecipeVo;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;

@Service
public class RecipeServiceImpl {

	 private Logger logger = LoggerFactory.getLogger(RecipeServiceImpl.class);
	 
	 public ResCommon<String> createSubRecipePic(RecipeVo recipeVo) {
		 ResCommon<String> response=new ResCommon<String>();
		 logger.info("开始生成处方单图片:"+JSON.toJSONString(recipeVo));
		 String fileName=DateUtils.dateToStringNoFlit(new Date())+".jpg";
		 String filePath="/data/recipe/image/";
		 File f=new File(filePath);
		 if(!f.isDirectory()&&!f.exists()){
			f.mkdirs();
		 }
		 String url=RecipePicUtil.createRecipe(recipeVo, filePath+fileName, null);
		 if(!StringUtils.isEmpty(url)){
			 response.setResultCodeAndMessageAndResult("0", "生成成功","{\"url\":\""+url+"\"}");
		 }else{
			 response.setResultCodeAndMessage("3000", "生成失败!");
		 }
		 logger.info("处方图片生成结束:"+JSON.toJSONString(response));
		 return response;
	 }
	 
	 public Response createRecipePic(String json, String sessionId) {
		 Response response=new Response();
		 
		 RecipeVo recipeVo=JsonUtils.parseObject(json,RecipeVo.class);
		 ResCommon<String> res=createSubRecipePic(recipeVo);
		 response.setResultCodeAndMessageAndResult(res.getResultCode(), res.getResultMessage(), res.getResult());
		 
		 return response;
	 }
	 
	 public static void main(String[] args){
		
		 String json="{\"hospital_name\":\"南山医院\",\"recipe_num\":\"123123123\",\"medical_card_no\":\"123123123\",\"name\":\"张三\",\"sex\":\"\",\"age\":\"31\",\"visiting_time\":\"2017-02-11 13:22:32\",\"dept_name\":\"\",\"queue_num\":\"333\",\"doctor_name\":\"陈培芬\",\"total_money\":\"9887.09\",\"rp_items\":[{\"item_name\":\"999感冒灵冲剂      10克*9袋/包包      1包\",\"item_unit\":\"用法: 开水冲服,一次一袋,一日3次。\"}]}";
		 RecipeVo recipeVo=JsonUtils.parseObject(json,RecipeVo.class);
		 
		 String url=RecipePicUtil.createRecipe(recipeVo, "d:\\222.png", null);
		
		 System.out.println(url);
	 }
}
